import SparkApi
import json

with open("Chat_data.json", "r", encoding="utf-8") as f:
    Data = json.load(f)

# 以下密钥信息从控制台获取
appid = Data["appid"]  # 填写控制台中获取的 APPID 信息
api_secret = Data["api_secret"]  # 填写控制台中获取的 APISecret 信息
api_key = Data["api_key"]  # 填写控制台中获取的 APIKey 信息

# 用于配置大模型版本，默认“general/generalv2”
# domain = "general"   # v1.5版本
# domain = "generalv2"    # v2.0版本
# 添加代码：-
domain = "generalv3"
# 云端环境的服务地址
# Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat"  # v1.5环境的地址
# Spark_url = "ws://spark-api.xf-yun.com/v2.1/chat"  # v2.0环境的地址
# 添加代码：
Spark_url = "ws://spark-api.xf-yun.com/v3.1/chat"

text = []


# length = 0

def getText(role, content):
    jsoncon = {}
    jsoncon["role"] = role
    jsoncon["content"] = content
    text.append(jsoncon)
    return text


def getlength(text):
    length = 0
    for content in text:
        temp = content["content"]
        leng = len(temp)
        length += leng
    return length


def checklen(text):
    while getlength(text) > 8000:
        del text[0]
    return text


def main(txt):
    try:
        text.clear
        i = 0
        while 1:
            Input = input("\n" + "我:")
            if i == 0:
                question = checklen(getText("user", (Input + "\n文件内容:" + txt)))
                i += 1
            else:
                question = checklen(getText("user", Input))
            SparkApi.answer = ""
            print("星火:", end="")
            SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)
            getText("assistant", SparkApi.answer)
            if Input in ["拜拜", "再见", "退出", "返回"]:
                break
            elif Input in ["换文档", "重新打开", "退出文档"]:
                pass
    except:
        return "拜拜"
